﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class CapquangService
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public CapquangService()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public decimal addorUpdate(CAPQUANG entity)
        {
            if (entity.ID == 0) dbContext.CAPQUANG.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.ID;
        }

        public decimal addorUpdate_SoiQuang(SOIQUANG entity)
        {
            if (entity.ID == 0) dbContext.SOIQUANG.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.ID;
        }

        public List<CAPQUANG> _listCapQuanByDVTOArray(string[] donvi, string[] toql)
        {
            try
            {
                var _listCapquang = new List<CAPQUANG>();
                _listCapquang = dbContext.CAPQUANG.Where(p => donvi.Contains(p.DONVIQL) && toql.Contains(p.TOQL)).ToList();
                return _listCapquang;

            }
            catch (Exception ex)
            {
                List<CAPQUANG> _listReturn = new List<CAPQUANG>(); return _listReturn;
            }
        }

        public List<CAPQUANG> _listCapQuanByDVTO(string donvi, string toql)
        {
            try
            {
                var _listCapquang = new List<CAPQUANG>();
                _listCapquang = dbContext.CAPQUANG.Where(p => (p.TOQL == toql || string.IsNullOrEmpty(toql) == null) && (p.DONVIQL == donvi || string.IsNullOrEmpty(donvi) == null)).ToList();
                return _listCapquang;

            }
            catch (Exception ex)
            {
                List<CAPQUANG> _listReturn = new List<CAPQUANG>(); return _listReturn;
            }
        }

        public List<SOIQUANG> _listSoiQuangByCapQuan(int capquangId)
        {
            try
            {
                var _listCapquang = new List<SOIQUANG>();
                _listCapquang = dbContext.SOIQUANG.Where(p => (p.CAPQUANGID == capquangId)).OrderBy(P => P.SOI.Value).ToList();
                return _listCapquang;

            }
            catch (Exception ex)
            {
                List<SOIQUANG> _listReturn = new List<SOIQUANG>(); return _listReturn;
            }
        }

        public CAPQUANG GetbyId(int ma)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.CAPQUANG.Where(p => p.ID == ma).First();
            }
            catch (Exception e)
            {
                CAPQUANG _listRe = new CAPQUANG();
                return _listRe;
            }
        }

        public CAPQUANG GetbyStringMa(string ma)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.CAPQUANG.Where(p => p.MA == ma).First();
            }
            catch (Exception e)
            {
                CAPQUANG _listRe = new CAPQUANG();
                return _listRe;
            }
        }

        public SOIQUANG GetSOIQUANGbyId(int ma)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.SOIQUANG.Where(p => p.ID == ma).First();
            }
            catch (Exception e)
            {
                SOIQUANG _listRe = new SOIQUANG();
                return _listRe;
            }
        }

        public List<CAPQUANG> GetCapQuangByQuanHuyenId_loaicap(string[] entityId, string[] loai_cap)
        {
            List<CAPQUANG> List_return = new List<CAPQUANG>();
            try
            {

                var entity = from i in dbContext.CAPQUANG
                             join k in dbContext.ODF on i.ODF1 equals k.ID
                             join m in dbContext.ODF on i.ODF2 equals m.ID
                             join h in dbContext.VNPT_SITE on k.SITEID equals h.SITEID
                             join l in dbContext.VNPT_SITE on m.SITEID equals l.SITEID
                             where (entityId.Contains(h.QUANHUYEN) && loai_cap.Contains(i.LOAICAP))
                             group i by i.ID into j
                             select new { key = j.Key, number = j };
                if (entity != null)
                {
                    var list_et = entity.ToList();

                    foreach (var item in list_et)
                    {
                        var ent = dbContext.CAPQUANG.SingleOrDefault(p => p.ID == item.key);
                        List_return.Add(ent);
                    }

                    return List_return;
                }
                else
                {
                    return null;
                }

            }
            catch { return null; }
        }
    }
}
